#include "UIButton.h"

namespace ds {

// =======================================================
// The button UI control implementation
// =======================================================

UIButton::UIButton(UIDialog *dialog,uint32 ID,const std::string& label) : UIControl(dialog,ID) , m_Label(label) {
	//Vec2 sp = m_Dialog->getAbsPos(pos);
	int diff = 20;
	m_Dialog->getTextNode()->setText(string::to_string(m_ID),Vec2(0,0),Color(1.0f,1.0f,1.0f,1.0f),"%s",label.c_str());
	m_Dialog->getTextNode()->calculateSize(string::to_string(m_ID),m_CalculatedSize);
	m_CalculatedSize.x += 40.0f;
	//m_BoundingBox.set(sp.y,sp.x,sp.x+m_CalculatedSize.x,sp.y+25.0f);
	m_CalculatedSize.y = 25.0f;
}


UIButton::~UIButton(void) {
}

void UIButton::align(const Vec2& gridPosition) {
	m_Dialog->getTextNode()->setText(string::to_string(m_ID),Vec2(gridPosition.x+20.0f,gridPosition.y+6.0f),Color(1.0f,1.0f,1.0f,1.0f),"%s",m_Label.c_str());
	m_BoundingBox.set(gridPosition.y,gridPosition.x,gridPosition.x+m_CalculatedSize.x,gridPosition.y+25.0f);
	m_Position = gridPosition;
}

bool UIButton::isSelected(const Vec2& mousePos) {
	Rect br = m_BoundingBox;
	if ( mousePos.x >= br.left && mousePos.x <= br.right && mousePos.y >= br.top && mousePos.y <= br.bottom ) {
		const BaseUIEvent be(UI_EVENT_BUTTON_CLICKED,m_ID);
		m_Dialog->sendEvent(be);
		return true;
	}
	return false;
}

void UIButton::fillBuffer(const Vec2& basePosition,ScreenQuad& buffer) {
	buffer.add(m_Position.x,m_Position.y,m_CalculatedSize.x,25.0f,Rect(120,260,100,25));
}

}